package com.tiles.controller;

import com.alibaba.fastjson.JSON;
import com.tiles.entity.TbDirection;
import com.tiles.entity.TbProfession;
import com.tiles.service.ProfessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import redis.clients.jedis.Jedis;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TilesController {
    @Autowired
    public ProfessionService professionService;

    @GetMapping("/profession")
    public String test1(HttpServletRequest request,Model model){

        Jedis jedis = new Jedis();

        if(jedis.exists("tbDirectionsList") && jedis.exists("professionsList")){
            String s = jedis.get("tbDirectionsList");
            String s1 = jedis.get("professionsList");
            List<TbDirection> tbDirections = JSON.parseArray(s, TbDirection.class);
            List<TbProfession> professions = JSON.parseArray(s1, TbProfession.class);
            //关闭redis
            jedis.close();
            model.addAttribute(professions);
            request.setAttribute("professions", professions);
            model.addAttribute(tbDirections);
            request.setAttribute("tbDirections",tbDirections);
            return "profession";
        }







        Example example = new Example(TbProfession.class);
        example.createCriteria().toString();
        example.orderBy("pid").asc();
        //所有职业列表
        List<TbProfession> professions = professionService.selectProfessionAll(example);
        //开发方向列表
        List<TbDirection> tbDirections = professionService.selectAll();
        //存入redis
        jedis.set("tbDirectionsList",JSON.toJSONString(tbDirections));
        jedis.set("professionsList", JSON.toJSONString(professions));
        //关闭redis连接
        jedis.close();

        model.addAttribute(tbDirections);
        request.setAttribute("tbDirections",tbDirections);

        model.addAttribute(professions);
        request.setAttribute("professions", professions);
        return "profession";
    }
    @GetMapping("/profession/{pid}")
    public String test2(@PathVariable String pid,HttpServletRequest request, Model model){

        List<TbDirection> tbDirections = professionService.selectAll();
        model.addAttribute(tbDirections);
        request.setAttribute("tbDirections",tbDirections);

        List<TbDirection> tbDirectionList=professionService.selectAll();
        model.addAttribute(tbDirectionList);
        Example example = new Example(TbProfession.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("pid",pid);
        List<TbProfession> professions = professionService.selectByPid(example);
        model.addAttribute(professions);
        request.setAttribute("professions", professions);
        return "profession";
    }

}
